package com.neusoft.nep.PublicSupervisor.controller;
import com.neusoft.nep.common.FileUtils;
import com.neusoft.nep.entity.Feedback;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.Map;

public class Feedbacklistcontroller {
    @FXML
    private TableView<Feedback> tableview;
    @FXML
    private TableColumn<Feedback, Integer> col_index;
    @FXML
    private TableColumn<Feedback, String> col_time;
    @FXML
    private TableColumn<Feedback, String> col_province;
    @FXML
    private TableColumn<Feedback, String> col_city;
    @FXML
    private TableColumn<Feedback, String> col_aqiLevel;
    @FXML
    private Button btnBack;
    private ObservableList<Feedback> feedlist = FXCollections.observableArrayList();

    @FXML
    public void initialize() {
        col_index.setCellValueFactory(new PropertyValueFactory<>("index"));
        col_time.setCellValueFactory(new PropertyValueFactory<>("time"));
        col_province.setCellValueFactory(new PropertyValueFactory<>("province"));
        col_city.setCellValueFactory(new PropertyValueFactory<>("city"));
        col_aqiLevel.setCellValueFactory(new PropertyValueFactory<>("aqiLevel"));

        tableview.setItems(feedlist);
        loadFeedbackData();
    }

    public void loadFeedbackData() {
        try {
            Map<String, Feedback> feedbackMap = FileUtils.readMapObject("feedback.txt");
            feedlist.clear();
            feedlist.addAll(feedbackMap.values());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void refreshList() {
        loadFeedbackData();
    }
    public void back() throws IOException {
        //获取Stage
        Stage stage = (Stage) btnBack.getScene().getWindow();
        //加载注册页面
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../View/Selectgrid.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = btnBack.getScene();
        scene.setRoot(root);
        stage.setScene(scene);
        stage.show();
    }
}